package com.adinnet.admin.dict.service.impl;

import com.adinnet.admin.dict.model.form.DictForm;
import com.adinnet.admin.dict.model.Dict;
import com.adinnet.admin.dict.mapper.DictMapper;
import com.adinnet.admin.dict.service.DictService;
import com.adinnet.common.utils.StringUtil;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典 服务实现类
 * </p>
 *
 * @author sunjianbo
 * @since 2019-11-05
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public PageResult<Dict> queryList(DictForm form) {
        Page<Dict> page = new Page<>(form.getPage(), form.getLimit());
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", form.getDictCode());
        IPage<Dict> iPage = baseMapper.selectPage(page, queryWrapper);
        return new PageResult<>(iPage.getRecords(), iPage.getTotal());
    }

    @Override
    public JsonResult deleteDict(String dictId) {
        Integer deleteCount = baseMapper.deleteById(dictId);
        if (deleteCount == 0) {
            return JsonResult.error("删除失败");
        }
        return JsonResult.ok("删除成功");
    }

    @Override
    public JsonResult addDict(Dict dict) {
        Integer addCount = baseMapper.insert(dict);
        if (addCount == 0) {
            return JsonResult.error("新增失败");
        }
        return JsonResult.ok("新增成功");
    }

    @Override
    public JsonResult updateDict(Dict dict) {
        Integer updateCount = baseMapper.updateById(dict);
        if (updateCount == 0) {
            return JsonResult.error("修改失败");
        }
        return JsonResult.ok("修改成功");
    }

    @Override
    public List<Dict> getArea() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_code", "AREA");
        queryWrapper.eq("parent_id", -1);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public PageResult<Dict> noPageList(Dict dict) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", dict.getDictCode());
        List<Dict> list = baseMapper.selectList(queryWrapper);
        return new PageResult<>(list, list.size());
    }

    @Override
    public Dict getDictByDictCode(String dictCode) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_code", dictCode);
        List<Dict> dictList= baseMapper.selectList(queryWrapper);
        if(dictList!=null && dictList.size()>0){
            return dictList.get(0);
        }else{
            return null;
        }
    }

    /**
     * 获取字典列表
     * @param form
     * @return
     */
    @Override
    public PageResult<Dict> dictList(DictForm form) {
        Page<Dict> page = new Page<>(form.getPage(), form.getLimit());
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        if(!StringUtil.isBlank(form.getDictCode())){
            queryWrapper.like("dict_code",form.getDictCode());
        }
        if(!StringUtil.isBlank(form.getRemark())){
            queryWrapper.like("remark",form.getRemark());
        }
        IPage<Dict> iPage = baseMapper.selectPage(page, queryWrapper);
        return new PageResult<>(iPage.getRecords(), iPage.getTotal());
    }

    @Override
    public JsonResult updateBootPage(int checked) {
        Dict dict = baseMapper.selectOne(new QueryWrapper<Dict>().eq("dict_code", "BOOT_PAGE"));
        if (null != dict) {
            Dict updateDict = new Dict();
            updateDict.setId(dict.getId());
            updateDict.setSortNum(checked);
            baseMapper.updateById(updateDict);
        } else {
            Dict insertDict = new Dict();
            insertDict.setDictCode("BOOT_PAGE");
            insertDict.setDictName("是否展示启动页");
            insertDict.setSortNum(checked);
            baseMapper.insert(insertDict);
        }
        return JsonResult.ok("修改成功");
    }
}
